package cn.com.shadowless.xinning.receiver;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;

import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.model.JPushInfo;
import cn.com.shadowless.xinning.utils.BadgeUtils;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.activity.OrderMoreInfoActivity;
import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * The type Message receiver.
 *
 * @author sHadowLess
 */
public class MessageReceiver extends JPushMessageReceiver {

    private static final String TAG = "PushMessageReceiver";
    public static int badgeCount = 0;

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG, "[onMessage] " + customMessage);
        if (!Build.BRAND.equals(BadgeUtils.BRAND_XI_AO_MI) && !Build.BRAND.equals(BadgeUtils.BRAND_HONOR) && !Build.BRAND.equals(BadgeUtils.BRAND_HUA_WEI)) {
            BadgeUtils.setBadge(++badgeCount, context);
        }
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageOpened] " + message);
        if (Build.BRAND.equals(BadgeUtils.BRAND_HONOR) || Build.BRAND.equals(BadgeUtils.BRAND_HUA_WEI)) {
            JPushInterface.setBadgeNumber(context, 0);
        } else if (Build.BRAND.equals(BadgeUtils.BRAND_VI_VO)) {
            BadgeUtils.setBadge(0, context);
        }
        JPushInfo jPushInfo = new Gson().fromJson(message.notificationExtras, JPushInfo.class);
        String billCode = jPushInfo.getBillCode();
        String orderId = jPushInfo.getOrderId();
        String billName = jPushInfo.getBillName();
        String userName = PreferencesUtils.getString(MyApplication.getInstance(), "userName", "-1");
        Intent intent = new Intent(context, OrderMoreInfoActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("orderId", orderId);
        intent.putExtra("billCode", billCode);
        intent.putExtra("billName", billName);
        intent.putExtra("userName", userName);
        context.startActivity(intent);
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageArrived] " + message.toString());
        if (Build.BRAND.equals(BadgeUtils.BRAND_VI_VO)) {
            BadgeUtils.setBadge(++badgeCount, context);
        }
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageDismiss] " + message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG, "[onRegister] " + registrationId);
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG, "[onConnected] " + isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG, "[onCommandResult] " + cmdMessage);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    @Override
    public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
        super.onNotificationSettingsCheck(context, isOn, source);
        Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
    }
}
